package com.cli.apisdk.interceptor;

import com.github.lianjiatech.retrofit.spring.boot.interceptor.BasePathMatchInterceptor;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author: xiongmin
 * @since create by 2022/7/15 16:56
 */
@Component
public class TokenInterceptor extends BasePathMatchInterceptor {
    private String token;

    public void setToken(String token) {
        this.token = token;
    }

    @Override
    protected Response doIntercept(Chain chain) throws IOException {
        Request request = chain.request();

        HttpUrl httpUrl = request.url()
                .newBuilder()
                .addQueryParameter("key", token)
                .build();

        Request newReq = request.newBuilder()
                .url(httpUrl)
                .build();
        return chain.proceed(newReq);
    }
}
